package model

import (
	"gorm.io/gorm"
	"shopping_08_20/globale"
	"time"
)

type Order struct {
	Id         uint32         `gorm:"column:id;type:int UNSIGNED;comment:订单ID;primaryKey;not null;" json:"id"`               // 订单ID
	UserId     int32          `gorm:"column:user_id;type:int;comment:用户ID;not null;" json:"user_id"`                         // 用户ID
	ShopCartId int32          `gorm:"column:shop_cart_id;type:int;comment:购物车ID;not null;" json:"shop_cart_id"`             // 购物车ID
	Status     int8           `gorm:"column:status;type:tinyint(1);comment:状态 1:发货 2:交付 3:退单;not null;" json:"status"` // 状态 1:发货 2:交付 3:退单
	CreatedAt  time.Time      `gorm:"column:created_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"created_at"`
	UpdatedAt  time.Time      `gorm:"column:updated_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"updated_at"`
	DeletedAt  gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
}

func (o *Order) TableName() string {
	return "order"
}

// 生成订单
func (o *Order) CreatedOrder() error {
	return globale.DB.Create(&o).Error
}

// 查看全部订单记录
func (o *Order) UserGetAllOrder() (order []Order, err error) {
	err = globale.DB.Where("user_id = ?", o.UserId).Find(&order).Error
	return
}

// 订单状态更新
func (o *Order) UpdateOrder() error {
	return globale.DB.Updates(&o).Error
}

// 撤销未完成订单
func (o *Order) DeleteOrder() error {
	return globale.DB.Delete(&o).Error
}
